home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Technology Seed / Mac Tech Seed Feb '97.toast / OpenDoc 1.2b2c1 / Implementation / Memory / MemDebgM.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-13  |  3.5 KB  |  155 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        MemDebgM.cpp
  3.  
  4.     Contains:    Mac-specific memory management debug routines
  5.  
  6.     Owned by:    Jens Alfke, Vincent Lo, Nick Pilch, Michael Burbidge
  7.  
  8.     Copyright:    © 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <2>     9/13/96    jpa        1371387: Speed optimizations.
  13.          <5>      5/4/95    jpa        Abstracted out highest/lowest possible
  14.                                     address stuff [1246077]
  15.          <4>     1/12/95    jpa        Include LowMem.h [1210936]
  16.          <3>    10/24/94    jpa        Call StripAddress in BasicValidatePtr.
  17.                                     [1193659]
  18.          <2>     9/29/94    RA        1189812: Mods for 68K build.
  19.          <1>     9/14/94    jpa        first checked in
  20.     
  21.     In Progress:
  22. */
  23.  
  24.  
  25. #ifndef _MEMCNFIG_
  26. #include "MemCnfig.h"
  27. #endif
  28.  
  29.  
  30. #if MM_DEBUG
  31.  
  32.  
  33. #ifndef _MEMDEBG_
  34. #include "MemDebg.h"
  35. #endif
  36.  
  37. #ifndef _MEMMGRPV_
  38. #include "MemMgrPv.h"
  39. #endif
  40.  
  41. #ifndef _MEMHOOKS_
  42. #include "MemHooks.h"
  43. #endif
  44.  
  45. #ifndef __MEMORY__
  46. #include <Memory.h>        // Mac memory manager
  47. #endif
  48.  
  49. #ifndef __LOWMEM__
  50. #include <LowMem.h>
  51. #endif
  52.  
  53.  
  54. const size_t kMaxHandleBlockSize = 0x7FFFFFFF;
  55.  
  56.  
  57. //------------------------------------------------------------------------------
  58. // PtrInHeap
  59. //------------------------------------------------------------------------------
  60.  
  61. inline static mmboolean
  62. PtrInHeap( const void *p, THz heap )
  63. {
  64.     return (p >= &heap->heapData) && (p < heap->bkLim);
  65. }
  66.  
  67.  
  68. //------------------------------------------------------------------------------
  69. // BasicValidatePtr
  70. //------------------------------------------------------------------------------
  71.  
  72. const char*
  73. BasicValidatePtr( const void *p, mmboolean mustBeInHeap )
  74. {
  75.     // If we could be sure that p were in the app zone, we could be more
  76.     // stringent. But it might be in the System heap or in temp-mem.
  77.     
  78. #if 0
  79.     static THz tempZone = kMMNULL;
  80.     if (tempZone == kMMNULL)
  81.     {
  82.         // Find the temp zone by allocating a handle in it and then getting its zone:
  83.         OSErr err;
  84.         
  85.         Handle handle = ::TempNewHandle(2, &err);
  86.         MM_ASSERT(err == noErr && handle != kMMNULL);
  87.         
  88.         tempZone = ::HandleZone(handle);
  89.         MM_ASSERT(tempZone != kMMNULL);
  90.         
  91.         ::DisposeHandle(handle);
  92.     }
  93. #endif
  94.  
  95.     p = StripAddress((void*)p);
  96.     
  97.     if( p == kMMNULL )
  98.         return "is NULL";
  99. //    else if( (MMULong)p & 1 )
  100. //        return "is odd";
  101.     else if( (MMULong)p & 2 )
  102.         return "isn't 4-byte-aligned";
  103. #if 0
  104.     // This does not appear to work any longer with the Modern Memory Manager...
  105.     else if( mustBeInHeap && !PtrInHeap(p,ApplicationZone())
  106.                             && !PtrInHeap(p,SystemZone())
  107.                               && !PtrInHeap(p,tempZone) )
  108.         return "is not in a known Mac heap zone";
  109. #endif
  110.     else if( /*!mustBeInHeap &&*/ (p<LOWEST_POSSIBLE_ADDRESS() || p>HIGHEST_POSSIBLE_ADDRESS()) )
  111.         return "is outside the known universe";
  112.     else
  113.         return kMMNULL;
  114. }
  115.  
  116.  
  117. //------------------------------------------------------------------------------
  118. // MMValidateHandle
  119. //------------------------------------------------------------------------------
  120.  
  121. MMBoolean
  122. MMValidateHandle( MMHandle MMHandle )
  123. {
  124.     Handle h = (Handle) MMHandle;
  125.     
  126.     // Check handle itself as a pointer:
  127.     const char *err = BasicValidatePtr(h);
  128.     
  129.     if( err == kMMNULL && *h == kMMNULL )
  130.         err = "is purged";
  131.     
  132.     if( err ) {
  133.         MM_WARN("Handle %p %s!",h,err);
  134.         return kMMFalse;
  135.     }
  136.     
  137.     // Check master pointer for validity:
  138.     err = BasicValidatePtr(*h);
  139.     if( err ) {
  140.         MM_WARN("Handle %p's master %p %s!",h,*h,err);
  141.         return kMMFalse;
  142.     }
  143.     
  144.     // Check handle's size:
  145.     size_t size = GetHandleSize(h);
  146.     if( size > kMaxHandleBlockSize )
  147.         MM_WARN("Handle %p has bogus size %08lx!",h,size);
  148.     else if( MemError() != noErr )
  149.         MM_WARN("Handle %p caused err %d on GetHandleSize",h,MemError());
  150.     else
  151.         return kMMTrue;
  152.     return kMMFalse;
  153. }
  154.  
  155. #endif /*MM_DEBUG*/